% this script reads EGI binary simple data files
% The process is the following: convert the unprocessed EGI Netstation
% files to Netstation Binary
% simple files. Import the Binary simple files in EEGLAB 8.0.3.3, rename
% the subjects to make them anonymous, rename events, give more properties
% to events (on_demand versus streaming; session 1 or 2; position in each 
% session). For 5 out of 16 subject, 1 out of 200 event was removed because
% it corresponded to an artifact of the recording hardware (the total
% number of event was 201 for these 5 subjects).

subjectcodes = { 'c1' 'c2' 'c3' 'c4' 'c5' 'c6' 'c7' 'c8' 'nd1' 'nd2' 'nd3' 'nd4' 'nd5' 'nd6' 'nd7' 'nd8' };
medarray = [0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1];
  
for index = 1:length(subjectcodes) 
    filename = fullfile(subjectcodes{index}, [ subjectcodes{index} '.raw' ]);
    if exist(filename)
         EEG = pop_readegi(fullfile(subjectcodes{index},    [ subjectcodes{index} '.raw' ]), []);
    else EEG = pop_readsegegi(fullfile(subjectcodes{index}, [ subjectcodes{index} '_001.raw' ]));
    end;
    
    % add transition and test that all datasets have a total
    % of 50 events in each cate
    % -------------------------
    EEG = findtransitions( EEG );
    if ~isempty(strmatch(subjectcodes{index}, { 'c5' 'c8' 'nd3' 'nd6' 'nd8' }))
        
        if strcmpi(subjectcodes{index}, 'c5'), countstop = 2; end;
        if strcmpi(subjectcodes{index}, 'c8'), countstop = 4; end;
        if strcmpi(subjectcodes{index}, 'nd3'), countstop = 2; end;
        if strcmpi(subjectcodes{index}, 'nd6'), countstop = 2; end;
        if strcmpi(subjectcodes{index}, 'nd8'), countstop = 4; end;
        
        % remove spurious event and rename index for others
        % this only happened in 5 subjects and only removes 1 
        % event out of 200
        count  = 0;
        remove = 0;
        for ind = 1:length(EEG.event)
            if ~isempty(EEG.event(ind).indexinsession) && EEG.event(ind).indexinsession == 1
                count = count+1;
                remove = 1;
            end;
            if count == countstop && remove
                indrm = [ ind-1:ind+1 ];
            end;
            if count == countstop
                EEG.event(ind).indexinsession = EEG.event(ind).indexinsession-1;
                remove = 0;
            end;
        end;
        EEG.event(indrm) = [];
        
    end;
    
    % check consistency of events
    % ---------------------------
    tmpevent = [ EEG.event.indexinsession ];
    if length(unique( tmpevent )) ~= 50, error('Err'); end;
    tmpevent = tmpevent - repmat(1:50, [1 4]);
    if length(unique(tmpevent)) ~= 1, error('Err'); end;
    if length(tmpevent) > 200, error('Err'); end;
    
    EEG = pop_selectevent( EEG,  'type',{ 'DI19'}, 'renametype', 'blank', 'deleteevents', 'off');
    EEG = pop_selectevent( EEG,  'type',{ 'DI21'}, 'renametype', 'light', 'deleteevents', 'off');
    EEG = pop_selectevent( EEG,  'type',{ 'DI25'}, 'renametype', 'audio', 'deleteevents', 'off');
    EEG = pop_selectevent( EEG,  'type',{ 'DI29'}, 'renametype', 'both' , 'deleteevents', 'off');
    %EEG = pop_selectevent( EEG,  'type',{ 'DI49'}, 'renametype', 'response', 'deleteevents', 'off');
    EEG.setname   = subjectcodes{index};
    EEG.comment   = '';
    EEG.mygroup   = fastif(medarray(index) == 1, 'nondual', 'control');
    EEG.subject   = subjectcodes{index};
    
    % save continuous data file
    % -------------------------
    filenameset = fullfile(subjectcodes{index}, [ subjectcodes{index} '_continuous.set' ]);  
    if exist(subjectcodes{index}) == 0
        mkdir(subjectcodes{index})
    end;
    pop_saveset(EEG, filenameset);
end;
